package recommendation;

import hibernate.method.GameMethods;
import hibernate.model.GamesRate;
import hibernate.util.HibernateUtil;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.UserBasedRecommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class GameRecommendationList {

	public static void main(String[] args) {
		try {
			
			SessionFactory sessionFactory = HibernateUtil.getSessionAnnotationFactory();
			Session session= sessionFactory.openSession();
			session.beginTransaction();
			
			GameMethods gm = new GameMethods();
			List<GamesRate> list = new ArrayList<GamesRate>();
			list = gm.listAllGamesRates(sessionFactory, session);
			BufferedWriter bw = new BufferedWriter(new FileWriter("recommendationData/gamesTemp.csv"));
			
			
			for (GamesRate g: list){
				//System.out.println(g.getUser_id() + "," + g.getGame_id() + "," + g.getRate());
				bw.write(g.getUser_id() + "," + g.getGame_id() + "," + g.getRate() + ".0 \n");
			}
			bw.close();
			
			
			DataModel dm = new FileDataModel(new File("recommendationData/gamesTemp.csv"));
			
//			//ItemSimilarity sim = new LogLikelihoodSimilarity(dm);
//			TanimotoCoefficientSimilarity sim = new TanimotoCoefficientSimilarity(dm);
//			
//			GenericItemBasedRecommender recommender = new GenericItemBasedRecommender(dm, sim);
//			
//			//int x=0;
//			int userId = 2;
//			for(LongPrimitiveIterator items = dm.getItemIDs(); items.hasNext();) {
//				long itemId = items.nextLong();
//				List<RecommendedItem>recommendations = recommender.mostSimilarItems(itemId, 5); // wygeneruj 5 rekomendacje na podstawie oszacowanego podobie�stwa dla filmu 1itemId
//				//List<RecommendedItem>recommendations = recommender.recommend(userId, 2); // wygeneruj 1 rekomendacje na podstawie dodatkowego modelu dla uzytkownika userId
//				
//				for(RecommendedItem recommendation : recommendations) {
//					System.out.println(itemId + "," + recommendation.getItemID() + "," + recommendation.getValue());
//				}
//			//	x++;
//				//if(x>10) System.exit(1);
//			}
			
			
			UserSimilarity similarity = new PearsonCorrelationSimilarity(dm);
			UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, dm);
			UserBasedRecommender recommender = new GenericUserBasedRecommender(dm, neighborhood, similarity);
			List<RecommendedItem> recommendations = recommender.recommend(10, 3);
			for (RecommendedItem recommendation : recommendations) {
			  System.out.println(recommendation);
			}
			
			
//			session.getTransaction().commit();
		} catch (IOException e) {
			System.out.println("There was an error.");
			e.printStackTrace();
		} catch (TasteException e) {
			System.out.println("There was a Taste Exception");
			e.printStackTrace();
		}
			
	}
}


